package com.ebelter.btcomlib.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.ebelter.btcomlib.R;
import com.ebelter.btcomlib.utils.DipUtil;

import java.util.ArrayList;
import java.util.List;


public class LevelColorView extends View {
    public LevelColorView(Context context) {
        this(context,null);
    }

    public LevelColorView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LevelColorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }



    private List<Integer> colors;
    private Paint mPaint;

    private int mWidth;
    private int mHeight;
    private int interval;

    int sjxWidth = DipUtil.dip2px(getContext(),10);
    int sjxHeight = DipUtil.dip2px(getContext(),10);

    private int level = -1;

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {

        colors=new ArrayList<>();
        mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LevelColorView, defStyleAttr, 0);
        int count = array.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = array.getIndex(i);
            int color=-1;
            if (attr==R.styleable.LevelColorView_color1){
                color = array.getColor(attr, -1);
            }else if (attr==R.styleable.LevelColorView_color2){
                color = array.getColor(attr, -1);
            }else if (attr==R.styleable.LevelColorView_color3){
                color = array.getColor(attr, -1);
            }else if (attr==R.styleable.LevelColorView_color4){
                color = array.getColor(attr, -1);
            }else if (attr==R.styleable.LevelColorView_color5){
                color = array.getColor(attr, -1);
            }else if (attr==R.styleable.LevelColorView_color6){
                color = array.getColor(attr, -1);
            }

            if (color!=-1){
                colors.add(color);
            }
        }
        array.recycle();
    }


    public void setLevel(int level){

        if (colors==null||level<0||level>colors.size()){
            return;
        }
        this.level=level;
        invalidate();
    }



    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (changed) {
            //这里需要确定几个基本点，只有确定了xy轴原点坐标，第一个点的X坐标值及其最大最小值
            initBase();
        }

    }


    private void initBase() {
        mWidth=getMeasuredWidth();
        mHeight=getMeasuredHeight();
        interval= DipUtil.dip2px(getContext(),1);
    }
    Path path2=new Path();

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int top=0;
        int buttom= (int) (mHeight*15.0/42);

        int itemWidth = mWidth/colors.size();

        for (int i=0;i<colors.size();i++){
            mPaint.setColor(colors.get(i));

            int left=i*itemWidth+interval/2;
            if (i==0){
                left=0;
            }
            int right = (i+1)*itemWidth-interval/2;
            if (i==colors.size()-1){
             right=mWidth;
            }
            canvas.drawRect(left,top,right,buttom,mPaint);
        }

        if (level>=0&&level<colors.size()){
            //画三角形
            mPaint.setColor(colors.get(level));

            path2.reset();
            path2.moveTo((float) (itemWidth*(level+0.5)), (float) (mHeight*23.0/42));
            path2.lineTo((float) (itemWidth*(level+0.5)+sjxWidth),(float) (mHeight*23.0/42)+sjxHeight);
            path2.lineTo((float) (itemWidth*(level+0.5)-sjxWidth),(float) (mHeight*23.0/42)+sjxHeight);
            path2.close();
            canvas.drawPath(path2, mPaint);

        }






    }
}
